/**
 * 协议管理工具
 * 用于管理用户协议和隐私政策的同意状态
 */

/**
 * 检查用户是否已同意隐私政策
 * @returns {boolean} 是否已同意
 */
export function hasAgreedPrivacyPolicy() {
	try {
		const agreed = uni.getStorageSync('privacy_policy_agreed')
		return agreed === true
	} catch (error) {
		console.error('检查隐私政策同意状态失败:', error)
		return false
	}
}

/**
 * 检查用户是否已同意用户协议
 * @returns {boolean} 是否已同意
 */
export function hasAgreedUserAgreement() {
	try {
		const agreed = uni.getStorageSync('user_agreement_agreed')
		return agreed === true
	} catch (error) {
		console.error('检查用户协议同意状态失败:', error)
		return false
	}
}

/**
 * 检查用户是否已同意所有必要协议
 * @returns {boolean} 是否已同意所有协议
 */
export function hasAgreedAllAgreements() {
	return hasAgreedPrivacyPolicy() && hasAgreedUserAgreement()
}

/**
 * 获取隐私政策同意时间
 * @returns {string|null} 同意时间的ISO字符串，未同意则返回null
 */
export function getPrivacyPolicyAgreeTime() {
	try {
		return uni.getStorageSync('privacy_policy_agree_time') || null
	} catch (error) {
		console.error('获取隐私政策同意时间失败:', error)
		return null
	}
}

/**
 * 获取用户协议同意时间
 * @returns {string|null} 同意时间的ISO字符串，未同意则返回null
 */
export function getUserAgreementAgreeTime() {
	try {
		return uni.getStorageSync('user_agreement_agree_time') || null
	} catch (error) {
		console.error('获取用户协议同意时间失败:', error)
		return null
	}
}

/**
 * 记录隐私政策同意状态
 * @param {boolean} agreed 是否同意
 */
export function setPrivacyPolicyAgreed(agreed = true) {
	try {
		uni.setStorageSync('privacy_policy_agreed', agreed)
		if (agreed) {
			uni.setStorageSync('privacy_policy_agree_time', new Date().toISOString())
		} else {
			uni.removeStorageSync('privacy_policy_agree_time')
		}
	} catch (error) {
		console.error('设置隐私政策同意状态失败:', error)
	}
}

/**
 * 记录用户协议同意状态
 * @param {boolean} agreed 是否同意
 */
export function setUserAgreementAgreed(agreed = true) {
	try {
		uni.setStorageSync('user_agreement_agreed', agreed)
		if (agreed) {
			uni.setStorageSync('user_agreement_agree_time', new Date().toISOString())
		} else {
			uni.removeStorageSync('user_agreement_agree_time')
		}
	} catch (error) {
		console.error('设置用户协议同意状态失败:', error)
	}
}

/**
 * 清除所有协议同意状态
 */
export function clearAllAgreements() {
	try {
		uni.removeStorageSync('privacy_policy_agreed')
		uni.removeStorageSync('privacy_policy_agree_time')
		uni.removeStorageSync('user_agreement_agreed')
		uni.removeStorageSync('user_agreement_agree_time')
	} catch (error) {
		console.error('清除协议同意状态失败:', error)
	}
}

/**
 * 获取协议同意状态摘要
 * @returns {object} 协议状态摘要
 */
export function getAgreementSummary() {
	return {
		privacyPolicy: {
			agreed: hasAgreedPrivacyPolicy(),
			agreeTime: getPrivacyPolicyAgreeTime()
		},
		userAgreement: {
			agreed: hasAgreedUserAgreement(),
			agreeTime: getUserAgreementAgreeTime()
		},
		allAgreed: hasAgreedAllAgreements()
	}
}

/**
 * 检查协议是否需要更新
 * 如果协议版本更新，可能需要用户重新同意
 * @param {string} currentVersion 当前协议版本
 * @returns {boolean} 是否需要重新同意
 */
export function needsAgreementUpdate(currentVersion = '1.0.0') {
	try {
		const lastAgreedVersion = uni.getStorageSync('agreement_version') || '0.0.0'
		return lastAgreedVersion !== currentVersion
	} catch (error) {
		console.error('检查协议版本失败:', error)
		return true // 出错时默认需要重新同意
	}
}

/**
 * 更新协议版本
 * @param {string} version 协议版本
 */
export function updateAgreementVersion(version = '1.0.0') {
	try {
		uni.setStorageSync('agreement_version', version)
	} catch (error) {
		console.error('更新协议版本失败:', error)
	}
}

/**
 * 强制显示协议同意页面
 * 用于首次使用或协议更新时
 * @param {string} type 协议类型：'privacy' | 'agreement' | 'both'
 */
export function showAgreementPages(type = 'both') {
	switch (type) {
		case 'privacy':
			uni.navigateTo({
				url: '/pages/privacy-policy/privacy-policy'
			})
			break
		case 'agreement':
			uni.navigateTo({
				url: '/pages/user-agreement/user-agreement'
			})
			break
		case 'both':
		default:
			// 先显示隐私政策，用户同意后再显示用户协议
			uni.navigateTo({
				url: '/pages/privacy-policy/privacy-policy?next=agreement'
			})
			break
	}
}